﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace TienTeTuong
{
    public class Global : System.Web.HttpApplication
    {
        public static string WebStoreFolder;
        public static string HostName;
        public static string Encryptkey = "TienTeTuong2012";
        public static string ConnectionString;
        public static bool isActiveSite;
        public static int counter;
        protected void Application_Start(object sender, EventArgs e)
        {
            WebStoreFolder = ConfigurationSettings.AppSettings["WebStoreFolder"];
            HostName = ConfigurationSettings.AppSettings["HostName"];

            try
            {
                isActiveSite = true;
                ConnectionString = DataProvider.CreateConnectString();
                Common.iNumItemPerPage = int.Parse(ConfigurationSettings.AppSettings["numItemsPerPage"]);
                Common.iNumHotItemPerPage = int.Parse(ConfigurationSettings.AppSettings["numHotItemsPerPage"]);
                Common.numTopArticle = int.Parse(ConfigurationSettings.AppSettings["numTopArticle"]);
                Common.numTopHotProductDetail = int.Parse(ConfigurationSettings.AppSettings["numTopProduct"]);

                // Code that runs on application startup
                string path = Server.MapPath("~") + "\\count.txt";

                if (!File.Exists(path))
                {
                    File.WriteAllText(path, "0");
                }
                int.TryParse(File.ReadAllText(path), out counter);
                Application["Access"] = counter;


            }
            catch (Exception ex)
            {
                Logger.WriteLog(LogLevel.ERROR, ex + "\n" + ex.StackTrace);
            }

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("~") + "\\count.txt";
                // Code that runs when a new session is started
                if (Application["Online"] == null)
                    Application["Online"] = 1;
                else
                    Application["Online"] = (int)Application["Online"] + 1;

                Application["Access"] = (int)Application["Access"] + 1;
                File.WriteAllText(path, Application["Access"].ToString());
            }
            catch (Exception ex)
            {

             Logger.WriteLog(LogLevel.ERROR, ex +"\n"+ex.StackTrace);
            }



        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            try
            {
                var i = (int)Application["Online"];
                if (i > 1)
                    Application["Online"] = i - 1;

                string path = Server.MapPath("~") + "\\count.txt";
                File.WriteAllText(path, Application["Access"].ToString());
            }
            catch (Exception)
            {

                throw;
            }


        }
        protected void Application_End(object sender, EventArgs e)
        {
            // Code that runs on application shutdown
            try
            {
                string path = Server.MapPath("~") + "\\count.txt";

                File.WriteAllText(path, Application["Access"].ToString());
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}